﻿using System.Buffers;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using common;
using Letter.IO.Binary;
using Letter.Net.SSL;
using Letter.Net.Tcp;
using ssl_connection;

namespace ssl_client1;


class Program
{
    
    
    
    static async Task Main()
    {
        SslConnection? connection = null;

        var client = TcpProvider.Client();
        client.ConfigureChannelOptions(new TcpClientTransportOptions());
        client.ConfigureFeatureOptions(new TcpClientFeatureOptions());
        client.ConfigureConnection(() =>
        {
            connection = new SslConnection(BinaryOrder.BigEndian, new SSLClientOptions(SocketConfig.cert.GetNameInfo(X509NameType.DnsName, false)), stream =>
            {
                return new SslStream(stream, true, (sender, certificate, chain, sslPolicyErrors) => true);
            });

            return connection;
        });
        client.Build();
        await client.ConnectAsync(IPEndPoint.Parse(SslConnection.serverAddr));
        var bytesMessages = new BytesMessages();
        while (true)
        {
            Console.ReadLine();
            var bytes = bytesMessages.GetMessage();
            var buf = new ReadOnlySequence<byte>(bytes);
            connection.WriteAndPushAsync(ref buf);
        }
    }
}
